package com.NU.csi;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.net.Uri;
import java.util.Arrays;
import java.util.Date;

public class ConferenceSchedulerInterface extends ActionBarActivity 
{
	public class ConferenceInfo
	{
		public String name;
		public String location;
		public String attendeeFirstName;
		public String attendeeLastName;
		public String sidebarFirstName;
		public String sidebarLastName;
		public String meetingMinutes;
		public String actionItems;
		public String startDate;
		public String endDate;
		
		public ConferenceInfo(String name, String location, String attendeeFirstName,
							  String attendeeLastName, String sidebarFirstName, String sidebarLastName,
							  String meetingMinutes, String actionItems, String startDate, String endDate)
		{
			this.name = name;
			this.location = location;
			this.attendeeFirstName = attendeeFirstName;
			this.attendeeLastName = attendeeLastName;
			this.sidebarFirstName = sidebarFirstName;
			this.sidebarLastName = sidebarLastName;
			this.meetingMinutes = meetingMinutes;
			this.actionItems = actionItems;
			this.startDate = startDate;
			this.endDate = endDate;		
		}
	};
	
	public ConferenceInfo[] conferenceInfo = {
			new ConferenceInfo("C++ Convention", "111 W Harbor Dr, San Diego, CA 92101", "Bill", "Gomez", 
					"Andrew","Reiplinger", "New C++ techniques were presented to attendees.", 
					"Attendees will train their companies on the new C++ techniques.",
					"August 30, 2014", "August 31, 2014"),
			new ConferenceInfo("Java Convention", "800 W Katella Ave, Anaheim, CA 92802", "Katie", "Caballero", 
					"Andrew","Reiplinger", "New Java techniques were presented to attendees.", 
					"Attendees will train their companies on the new Java techniques",
					"August 30, 2014", "August 31, 2014")
				};
	
	Spinner conferenceElementsSpinner;
	Spinner conferenceNameSpinner;
	private TextView basicInfo;
	private TextView meetingMinutes;
	private TextView actionItems;
	private TextView sidebar;
	private EditText username;
	private EditText password;
	private int arrayPosition;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if(savedInstanceState == null)
		{
			arrayPosition = 0;
		}
		loginScreen();
	}
	
	public void loginScreen()
	{
		setContentView(R.layout.login_screen);
		
		username = (EditText) findViewById(R.id.usernameEditText);
		password = (EditText) findViewById(R.id.passwordEditText);
			
		Button login = (Button) findViewById(R.id.loginButton);
		
		Button exitButton = (Button) findViewById(R.id.exitButton);
		exitButton.setOnClickListener(exitButtonListener);
	}
	
	public void login(View view)
	{
	    if(username.getText().toString().equals("User") && 
	    password.getText().toString().equals("admin"))
	    {
	    	Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
	    	homeScreen();
	    }	
	    
	    else
	    {
	    	Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();
	    	username.setText("");
	    	password.setText("");
	    }
	}
	
	public void homeScreen()
	{
		setContentView(R.layout.home_screen);
		
		Button createConference = (Button) findViewById(R.id.createConferenceButton);
		createConference.setOnClickListener(createConferenceButtonListener);
		
		Button deleteConference = (Button) findViewById(R.id.deleteConferenceButton);
		deleteConference.setOnClickListener(deleteConferenceButtonListener);
		
		Button viewConference = (Button) findViewById(R.id.viewConferenceButton);
		viewConference.setOnClickListener(viewConferenceButtonListener);
		
		Button logout = (Button) findViewById(R.id.logoutButton);
		logout.setOnClickListener(logoutButtonListener);
	}
	
	public void createConferenceScreen()
	{
		setContentView(R.layout.conference_creation_screen);
		
		Button returnHome = (Button) findViewById(R.id.returnHomeMenuButton);
		returnHome.setOnClickListener(returnHomeButtonListener);
		
		Button confirmEntries = (Button) findViewById(R.id.confCreateConfirmButton);
		confirmEntries.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				attendeeListScreen();			
			}
		});
	}
	
	public void attendeeListScreen()
	{
		setContentView(R.layout.attendee_list_screen);
		
		Button returnHome = (Button) findViewById(R.id.returnHomeMenuButton);
		returnHome.setOnClickListener(returnHomeButtonListener);
	}
	
	public void deleteConferenceScreen()
	{
		setContentView(R.layout.conference_removal_screen);
		
		Button returnHome = (Button) findViewById(R.id.returnHomeMenuButton);
		returnHome.setOnClickListener(returnHomeButtonListener);
	}
	
	public void viewConferenceScreen()
	{
		setContentView(R.layout.conference_selection_screen);
		
		conferenceNameSpinner = (Spinner) findViewById(R.id.confListSpinner);
		
		ArrayAdapter<CharSequence> confNameAdapter = ArrayAdapter.createFromResource(this,
		        R.array.conferenceNames, android.R.layout.simple_spinner_item);
		
		confNameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		conferenceNameSpinner.setAdapter(confNameAdapter);
		
		Button returnHome = (Button) findViewById(R.id.returnHomeMenuButton);
		returnHome.setOnClickListener(returnHomeButtonListener);
		
		conferenceNameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
			{
				if(position == 1)
				{
					arrayPosition = 0;
					conferenceMainMenuScreen();
				}
				
				else if(position == 2)
				{
					arrayPosition = 1;
					conferenceMainMenuScreen();
				}
			}
			
			@Override
		    public void onNothingSelected(AdapterView<?> parent) 
		    {
		        // Nothing happens
		    }
		});
	}
	
	public void logout()
	{
		loginScreen();
	}
	
	public void returnHome()
	{
		homeScreen();
	}
	
	public void conferenceMainMenuScreen()
	{
		setContentView(R.layout.conference_main_menu_screen);
		
		conferenceElementsSpinner = (Spinner) findViewById(R.id.confElementsSpinner);
		
		ArrayAdapter<CharSequence> elementAdapter = ArrayAdapter.createFromResource(this,
		        R.array.conferenceElements, android.R.layout.simple_spinner_item);
		
		elementAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		conferenceElementsSpinner.setAdapter(elementAdapter);
		
		Button returnHome = (Button) findViewById(R.id.returnHomeMenuButton);
		returnHome.setOnClickListener(returnHomeButtonListener);
		
		conferenceElementsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
			{
				if(position == 1)
				{
					conferenceBasicInfoScreen();
				}
				
				else if(position == 2)
				{
					localHotelScreen();
				}
				
				else if(position == 3)
				{
					localRestaurantScreen();
				}
				
				else if(position == 4)
				{
					localRentalCarScreen();
				}
				
				else if(position == 5)
				{
					localEntertainmentScreen();
				}
				
				else if(position == 6)
				{
					meetingMinutesUpdateScreen();
				}
				
				else if(position == 7)
				{
					meetingMinutesScreen();
				}
				
				else if(position == 8)
				{
					sidebarCreationScreen();
				}
				
				else if(position == 9)
				{
					sidebarScreen();
				}
			}
			
			@Override
		    public void onNothingSelected(AdapterView<?> parent) 
		    {
		        // Nothing happens
		    }
		});
	}
	
	public void conferenceBasicInfoScreen()
	{
		setContentView(R.layout.conference_basic_information_screen);
		
		Button viewBasicMap = (Button) findViewById(R.id.viewBasicMapButton);
		viewBasicMap.setOnClickListener(viewBasicMapButtonListener);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
		
		basicInfo = (TextView) findViewById(R.id.basicInfoTextView);
		
		basicInfo.setText("Name: " + "\n" + conferenceInfo[arrayPosition].name + "\n\n" + 
				  		  "Location:" + "\n" + conferenceInfo[arrayPosition].location + "\n\n" +
				  		  "Start Date: " + "\n" + conferenceInfo[arrayPosition].startDate + "\n\n" +
						  "End Date: " + "\n" + conferenceInfo[arrayPosition].endDate);
	}
		
	public void localHotelScreen()
	{
		setContentView(R.layout.local_hotel_screen);
		
		Button viewHotelMap = (Button) findViewById(R.id.viewHotelMapButton);
		viewHotelMap.setOnClickListener(viewHotelMapButtonListener);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void localEntertainmentScreen()
	{
		setContentView(R.layout.local_entertainment_screen);
		
		Button viewEntertainmentMap = (Button) findViewById(R.id.viewEntertainmentMapButton);
		viewEntertainmentMap.setOnClickListener(viewEntertainmentMapButtonListener);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void localRentalCarScreen()
	{
		setContentView(R.layout.local_rental_car_screen);
		
		Button viewRentalCarMap = (Button) findViewById(R.id.viewRentalCarMapButton);
		viewRentalCarMap.setOnClickListener(viewRentalCarMapButtonListener);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void localRestaurantScreen()
	{
		setContentView(R.layout.local_restaurant_screen);
		
		Button viewRestaurantMap = (Button) findViewById(R.id.viewRestaurantMapButton);
		viewRestaurantMap.setOnClickListener(viewRestaurantMapButtonListener);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void meetingMinutesUpdateScreen()
	{
		setContentView(R.layout.meeting_minutes_update_screen);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void meetingMinutesScreen()
	{
		setContentView(R.layout.meeting_minutes_screen);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
		
		meetingMinutes = (TextView) findViewById(R.id.meetingMinutesTextView2);
		actionItems = (TextView) findViewById(R.id.actionItemsTextView2);
		
		meetingMinutes.setText(conferenceInfo[arrayPosition].meetingMinutes);
		actionItems.setText(conferenceInfo[arrayPosition].actionItems);
	}
	
	public void sidebarCreationScreen()
	{
		setContentView(R.layout.sidebar_update_screen);

		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void sidebarScreen()
	{
		setContentView(R.layout.sidebar_screen);
		
		sidebar = (TextView) findViewById(R.id.sidebarInfoTextView);
		
		sidebar.setText("Location:" + "\n" + conferenceInfo[arrayPosition].location + "\n\n" +
  		        "Attendees:" + "\n" + conferenceInfo[arrayPosition].attendeeFirstName + " " +
		        conferenceInfo[arrayPosition].attendeeLastName + "\n" +
		        conferenceInfo[arrayPosition].sidebarFirstName + " " +
		        conferenceInfo[arrayPosition].sidebarLastName);
		
		Button viewSidebarMap = (Button) findViewById(R.id.viewSidebarMapButton);
		viewSidebarMap.setOnClickListener(viewSidebarMapButtonListener);
		
		Button returnMainMenu = (Button) findViewById(R.id.returnMainMenuButton);
		returnMainMenu.setOnClickListener(returnMainMenuButtonListener);
	}
	
	public void returnMainMenu()
	{
		conferenceMainMenuScreen();
	}
	
	public void googleMaps(String location, String search, boolean isBasic)
	{
		if(isBasic)
		{
			startActivity(new Intent(Intent.ACTION_VIEW, 
					Uri.parse("http://www.google.com/maps/search/" + location)));
		}
		
		else if(!isBasic)
		{
			startActivity(new Intent(Intent.ACTION_VIEW, 
					Uri.parse("http://www.google.com/maps/search/" + search + location)));
		}
		
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) 
	{
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) 
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
	
	public OnClickListener exitButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			// Application Terminates
			System.exit(0);
		}
	};
	
	public OnClickListener createConferenceButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			createConferenceScreen();
		}
	};
	
	public OnClickListener deleteConferenceButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			deleteConferenceScreen();
		}
	};
	
	public OnClickListener viewConferenceButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			viewConferenceScreen();
		}
	};
	
	public OnClickListener logoutButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			logout();
		}
	};
	
	public OnClickListener returnHomeButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			returnHome();
		}
	};
	
	public OnClickListener returnMainMenuButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			returnMainMenu();
		}
	};
	
	public OnClickListener viewBasicMapButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			googleMaps(conferenceInfo[arrayPosition].location, "", true);
		}
	};
	
	public OnClickListener viewHotelMapButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			googleMaps(conferenceInfo[arrayPosition].location, "hotels near ", false);
		}
	};
	
	public OnClickListener viewEntertainmentMapButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			googleMaps(conferenceInfo[arrayPosition].location, "entertainment near ", false);
		}
	};
	
	public OnClickListener viewRentalCarMapButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			googleMaps(conferenceInfo[arrayPosition].location, "rental car near ", false);
		}
	};
	
	public OnClickListener viewRestaurantMapButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			googleMaps(conferenceInfo[arrayPosition].location, "restaurants near ", false);
		}
	};
	
	public OnClickListener viewSidebarMapButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			googleMaps(conferenceInfo[arrayPosition].location, " ", false);
		}
	};
}